<?php
// app\admin_v1\应用下的公共函数文件
//  2023-7-2
// header_remove("X-Powered-By"); //删除响应头
// 生成guid 唯一识别码
if (function_exists('common__guid')) {
    return common__guid(); //已定义
} else {
    function common__guid()
    {
        // 输出格式：
        // $var = [
        //     "salt" => "462F545598BE255976CA5A7D9A901DA7",
        //     "salt_length" => 32,
        //     "salt_any" => "462F-98BE-2559-76CA-5A7D",
        //     "salt_any_length" => 24
        // ];
        mt_srand((float) time() * 10000);
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $hyphen = chr(45); //"-"
        $uuid = substr($charid, 0, 4) . $hyphen
            . substr($charid, 8, 4) . $hyphen
            . substr($charid, 12, 4) . $hyphen
            . substr($charid, 16, 4) . $hyphen
            . substr($charid, 20, 4);
        $salt_any = [
            "salt" => $charid,
            "salt_length" => strlen($charid),
            "salt_any" => $uuid,
            "salt_any_length" => strlen($uuid),
        ];
        return $salt_any;
    }
}
//curl封装
if (!function_exists('common__curl')) {
    function common__curl($url)
    {
        $headerArray = array("Content-type:application/json;", "Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output, true);
        return $output;
    }
}


// 当前已登录会话用户的缓存文件
if (!function_exists('common__user_cache')) {
    /** 当前已登录会话用户的缓存文件 */
    function common__user_cache()
    {
        if (request()->header("token")) {
            return cache(md5(md5(app("http")->getName() . request()->ip()) . request()->header("token")));
        } else {
            return null;
        }
    }
}
